package com.thd.kc.utils;

import com.kennycason.kumo.nlp.tokenizer.WordTokenizer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class MyTokenizer implements WordTokenizer {
    private IKAnalyzer tokenizer = new IKAnalyzer();
    public List<String> tokenize(String sentence) {
        try{
            Analyzer thisAnalysis = new IKAnalyzer();
            StringReader reader = new StringReader(sentence);
            TokenStream ts = thisAnalysis.tokenStream("", reader);
            System.out.println(ts);
            CharTermAttribute termAtt = ts.getAttribute(CharTermAttribute.class);
            ts.reset();
            List<String> r = new ArrayList<String>();
            System.out.println("======================  关键词分词结果： ======================");
            while (ts.incrementToken()) {
                String aword = termAtt.toString();
                r.add(aword);
                System.out.println(aword);
            }



            return r;
        }catch (Exception e){
            System.out.print(e.getMessage());
            return new ArrayList<String>();
        }



    }
}
